<?php
/**
 * Widget form.
 *
 * @package    sinersis
 * @subpackage form
 * @author     Alejandro Hurtado
 * @version    SVN: $Id: sfWidgetFormInputFlashFile
 * Idea taken from  sfWidgetFormInputFileEditable widget of Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 */
/**
 *  class widget for flash objects
 *
 */
class sfWidgetFormInputFlashFile extends sfWidgetFormInputFile {

    /**
     * Constructor.
     *
     * Available options:
     *
     *  * file_src:     The current flash web source path (required)
     *  * edit_mode:    A Boolean: true to enabled edit mode, false otherwise
     *  * is_image:     Whether the file is a displayable image
     *  * is_flash:     Whether the file is a flash object
     *  * flash_width
     *  * flash_height
     *  * quality
     *  * bgcolor
     *  * with_delete:  Whether to add a delete checkbox or not
     *  * delete_label: The delete label used by the template
     *  * template:     The HTML template to use to render this widget when in edit mode
     *                  The available placeholders are:
     *                    * %input% (the image upload widget)
     *                    * %delete% (the delete checkbox)
     *                    * %delete_label% (the delete label text)
     *                    * %file% (the file tag)
     *
     * In edit mode, this widget renders an additional widget named after the
     * file upload widget with a "_delete" suffix. So, when creating a form,
     * don't forget to add a validator for this additional field.
     *
     * @param array $options     An array of options
     * @param array $attributes  An array of default HTML attributes
     *
     * @see sfWidgetFormInputFile
     */
    protected function configure($options = array(), $attributes = array()) {
        parent::configure($options, $attributes);

        $this->setOption('type', 'file');
        $this->setOption('needs_multipart', true);

        $this->addRequiredOption('file_src');

        $this->addOption('is_image', false);

        $this->addOption('is_flash', false);

        $this->addRequiredOption('movie');
        $this->addOption('flash_width', '100%');
        $this->addOption('flash_height', '100%');
        $this->addOption('quality','high');
        $this->addOption('bgcolor','#FFFFFF' );
        $this->addOption('pluginspage', 'http://www.macromedia.com/go/getflashplayer' );
        $this->addOption('classid', 'classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\'');
        $this->addOption('codebase', 'codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\' ');
//            $this->addOption('id','');

        $this->addOption('edit_mode', true);
        $this->addOption('with_delete', true);
        $this->addOption('delete_label', 'remove the current file');
        $this->addOption('template', '%file%<br />%input%<br />%delete% %delete_label%');
    }

    /**
     * @param  string $name        The element name
     * @param  string $value       The value displayed in this widget
     * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
     * @param  array  $errors      An array of errors for the field
     *
     * @return string An HTML tag string
     *
     * @see sfWidgetForm
     *
     */
    public function render($name, $value = null, $attributes = array(), $errors = array()) {
        $input = parent::render($name, $value, $attributes, $errors);

        if (!$this->getOption('edit_mode')) {
            return $input;
        }

        if ($this->getOption('with_delete')) {
            $deleteName = ']' == substr($name, -1) ? substr($name, 0, -1) . '_delete]' : $name . '_delete';

            $delete = $this->renderTag('input', array_merge(array('type' => 'checkbox', 'name' => $deleteName), $attributes));
            $deleteLabel = $this->translate($this->getOption('delete_label'));
            $deleteLabel = $this->renderContentTag('label', $deleteLabel, array_merge(array('for' => $this->generateId($deleteName))));
        } else {
            $delete = '';
            $deleteLabel = '';
        }

        return strtr($this->getOption('template'), array('%input%' => $input, '%delete%' => $delete, '%delete_label%' => $deleteLabel, '%file%' => $this->getFileAsTag($attributes)));
    }

    /**
     * auxiliar method for render
     *
     * @param array $attributes
     * @return string
     */
    protected function getFileAsTag($attributes) {              
        if ($this->getOption('is_image')) {
            return false !== $this->getOption('file_src') ? $this->renderTag('img', array_merge(array('src' => $this->getOption('file_src')), $attributes)) : '';
        } elseif ($this->getOption('is_flash')) {
            return false !== $this->getOption('file_src') ?$this->renderFlashTag(
                 $this->getOption('file_src'),
                   array(
                'movie'=>$this->getOption('movie'),
                'quality'=>$this->getOption('quality'),
                'bgcolor'=>$this->getOption('bgcolor'),
                'type'=>$this->getOption('type'),
                'pluginspage'=>$this->getOption('pluginspage'),
            ),array(
                'id'=>"flashobj",
                'classid'=>$this->getOption('classid'),
                'codebase'=>$this->getOption('codebase'),
                'width'=>$this->getOption('flash_width'),
                'height'=>$this->getOption('flash_height'),
            ) ):'';
        } else {
            return $this->getOption('file_src');
        }
    }

    /**
     * render html object and embed needed for flash object
     *
     * @param string $src
     * @param array $params
     * @param array $attribs
     * @return string
     */
    protected function renderFlashTag($src, $params=array(), $attribs=array() ) {
        //object for ie
        $s = '<OBJECT '; 
        foreach ($attribs as $attrib => $value)
        {
            $s.= ' '.$attrib.'="'.$value.'"';
        }
        $s.=' >';
        foreach ($params as $name=>$value)
        {
           $s .=  '<PARAM NAME='.$name.' VALUE="'.$value.'">';
        }
        // embed for mozilla
        $opt=array(
            'src'=>$src,
            'quality'=>$params['quality'],
            'bgcolor'=>$params['bgcolor'],
            'width'=> $attribs['width'],
            'height'=>$attribs['height'],
            'name'=>$params['movie'],
            'ALIGN'=>"",
            'TYPE'=>"application/x-shockwave-flash",
            );
        if($params['pluginspage'])
        {
            $opt['PLUGINSPAGE'] = $params['pluginspage'];
        }
        
        $s .= $this->renderTag('EMBED',  $opt );
        $s.='</OBJECT>';
        return $s;
    }


}
